//
//  OACityNameViewController.m
//  OASystem
//
//  Created by zhoupengfei on 15/8/12.
//  Copyright (c) 2015年 zpf. All rights reserved.
//

#import "OACityNameViewController.h"
#import "HttpTools.h"

#import "GDataDefines.h"
#import "MBProgressHUD.h"
#import "OANameListModel.h"
#import "OASubNameListModel.h"
#import "HeaderView.h"
#import "OANameListController.h"
@interface OACityNameViewController ()<OANameListControllerDelegate>
@property(nonatomic,strong) OANameListModel * titleModel;
@property(nonatomic,strong)NSMutableArray * parentArray;

@property(nonatomic,strong)NSMutableArray * groupArray; //group
@property(nonatomic,strong)NSMutableArray * rowArray;
@end

@implementation OACityNameViewController

-(NSMutableArray*)parentArray{
    if (_parentArray == nil) {
        _parentArray = [NSMutableArray array];
    }
    return _parentArray;
}

-(NSMutableArray*)groupArray{
    
    if (_groupArray == nil) {
        _groupArray = [NSMutableArray array];
    }
    return _groupArray;
}

-(NSMutableArray*)rowArray{
    
    if (_rowArray == nil) {
        _rowArray = [NSMutableArray array];
    }
    return _rowArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
}

#pragma mark 加载网络数据
-(void)loadData{
    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"userId":userId}];
    [HttpTools postWithURL:KexchmailURL arrays:array method:KReturnTreeMethod success:^(id JSON) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        [self getSubjectTitles:[JSON dataUsingEncoding:NSUTF8StringEncoding]];
        self.title = self.titleModel.value;
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];
    
}

-(void)getSubjectTitles:(NSData*)data
{
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: data options:0 error:nil];
    //获取根节点
    GDataXMLElement *rootElement = [doc rootElement];
  
    NSArray *ParentNodeData= [rootElement elementsForName:@"ParentNode"];
    for (int i = 0;i<[ParentNodeData count];i++)
    {
        GDataXMLElement *ParentElement = [ParentNodeData objectAtIndex:i];
        NSArray *ParentData= [ParentElement elementsForName:@"Parent"];
        for (int j = 0; j<[ParentData count]; j++)
        {
            OANameListModel * titleModel = [[OANameListModel alloc]init];
            GDataXMLElement *element = [ParentData objectAtIndex:j];
            titleModel.uid = [[element attributeForName:@"uid"] stringValue];
            titleModel.value = [[element attributeForName:@"value"]stringValue];
            [self.parentArray addObject:titleModel];
        }
    }
    
    NSArray *PartNodeData= [rootElement elementsForName:@"PartNode"];
    for (int i = 0;i<[PartNodeData count];i++)
    {
        GDataXMLElement *PNodeElement = [PartNodeData objectAtIndex:i];
        NSArray *PNodeData= [PNodeElement elementsForName:@"PNode"];
        for (int j = 0; j<[PNodeData count]; j++)
        {
            OANameListModel * subjectModel = [[OANameListModel alloc]init];
            GDataXMLElement *element = [PNodeData objectAtIndex:j];
            subjectModel.pid = [[element attributeForName:@"pid"] stringValue];
             subjectModel.uid = [[element attributeForName:@"uid"] stringValue];
            subjectModel.value = [[element attributeForName:@"value"]stringValue];
            subjectModel.subArray  = [NSMutableArray array];
            [self.groupArray addObject:subjectModel];
        }
    }
    
    NSArray *PersonNodeData= [rootElement elementsForName:@"PersonNode"];
    for (int i = 0;i<[PersonNodeData count];i++)
    {
        GDataXMLElement *PersonElement = [PersonNodeData objectAtIndex:i];
        NSArray *PersonData= [PersonElement elementsForName:@"Person"];
        for (int j = 0; j<[PersonData count]; j++)
        {
            OASubNameListModel * subModel = [[OASubNameListModel alloc]init];
            GDataXMLElement *element = [PersonData objectAtIndex:j];
            subModel.pid = [[element attributeForName:@"pid"] stringValue];
            subModel.value = [[element attributeForName:@"value"]stringValue];
            [self.rowArray addObject:subModel];
        }
    }
    
    for (OANameListModel * nameLisModel in self.groupArray) {
        for (OASubNameListModel * subModel in self.rowArray) {
            if ([nameLisModel.uid isEqualToString:subModel.pid]) {
                [nameLisModel.subArray addObject:subModel];
            }
        }
    }
    [self.tableView reloadData];
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.parentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * cellId = @"CellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    OANameListModel * nameListModel = self.parentArray[indexPath.row];
    cell.textLabel.text = nameListModel.value;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OANameListModel * parentModel = self.parentArray[indexPath.row];
    NSMutableArray * goupArray = [NSMutableArray array];
    for (OANameListModel * listModel in self.groupArray) {
        if ([parentModel.uid isEqualToString:listModel.pid]) {
            [goupArray addObject:listModel];
        }
    }
    
    UIStoryboard * stroyboard =    [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    OANameListController * listVC = (OANameListController*)[stroyboard instantiateViewControllerWithIdentifier:@"NameLIstID"];
        listVC.delegate = self;
         listVC.groupArray = goupArray;
    [self.navigationController pushViewController:listVC animated:YES];

}
-(void)didSelecteName:(NSArray*)names{
    NSMutableArray * namesArray = [NSMutableArray array];
    for (OASubNameListModel * model in names) {
        [namesArray addObject:model.value];
    }
    if (self.GetNameStringBlock) {
        self.GetNameStringBlock([namesArray componentsJoinedByString:@","]);
    }

}


@end
